#!/bin/bash

PROJECT_NAME=northwind

CONTEXT_ROOT=$PROJECT_NAME
WAR_FILE=$PROJECT_NAME'.war'
DOWNLOAD_PATH='http://192.168.12.115:8080/jenkins/job/'$PROJECT_NAME'/ws/build/'$WAR_FILE

TOMCAT_HOME=/var/lib/tomcat6

RETVAL=0

deploy() {
	rm -f $WAR_FILE
	wget $DOWNLOAD_PATH
	
	sudo -A mv $WAR_FILE $TOMCAT_HOME'/webapps/'$WAR_FILE
	rm -f $WAR_FILE
}

undeploy() {
	sudo -A rm -rf $TOMCAT_HOME'/webapps/'$WAR_FILE
	sudo -A rm -rf $TOMCAT_HOME'/webapps/'$CONTEXT_ROOT
}

start() {
	sudo -A /etc/init.d/tomcat6 start
}

stop() {
	sudo -A /etc/init.d/tomcat6 stop
}

case "$1" in
	deploy)
		deploy
		;;
	undeploy)
		undeploy
		;;
	redeploy)
		undeploy
		deploy
		;;
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		stop
		start
		;;
	*)
		echo $"Usage: $0 {start|stop|restart|deploy|undeploy|redeploy}"
		RETVAL=1
esac
exit $RETVAL
